#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>

#include "bsHeap.h"

typedef struct _test_data_t
{
    int num;
}data_t;

E_CMP_t cmp(void* d1, void* d2)
{
    if (NULL == d1 || NULL ==d2)
        return E_CMP_ERROR;

    if(((data_t*)d1)->num > ((data_t*)d2)->num)
    {
        return E_CMP_GREATER;
    }
    else if(((data_t*)d1)->num < ((data_t*)d2)->num)
    {
        return E_CMP_LESS;
    }
    else
    {
        return E_CMP_EQUAL;
    }
}

int main()
{
    srand(time(NULL));

    BS_HEAP_t* bsHeap = bsHeapCreate(100, 10000, cmp);
    if (bsHeap == NULL)
    {
        printf("create bs heap error\n");
    }
    data_t d;
    int cnt = 0;
    for(int i = 0; i < 100; i++)
    {
        d.num = rand()%1000;

        if( 0 != bsHeap->add(bsHeap, (void*)&d, sizeof(data_t)))
        {
            printf("add error %d\n", d.num);
        }
        cnt++;
    }
   
    void* data = bsHeap->pop(bsHeap);

    while(data != NULL)
    {
        cnt--;
        printf("pop:%d\n", ((data_t*)data)->num);
        free(data);

        data = bsHeap->pop(bsHeap);
    }
    printf("cnt remain:%d\n", cnt);

    bsHeap->destruct(&bsHeap);
}

